package com.hoshiicloud.user.service.impl.platform;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hoshiicloud.common.aop.Logable;
import com.hoshiicloud.common.exception.CommonError;
import com.hoshiicloud.common.exception.ServiceException;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.common.utils.BeanUtils;
import com.hoshiicloud.user.entity.company.Company;
import com.hoshiicloud.user.feign.IdGeneratorService;
import com.hoshiicloud.user.entity.platform.Platform;
import com.hoshiicloud.user.mapper.platform.PlatformMapper;
import com.hoshiicloud.user.param.platform.PlatFormInsertParam;
import com.hoshiicloud.user.param.platform.PlatFormParam;
import com.hoshiicloud.user.param.platform.PlatFormUpdateParam;
import com.hoshiicloud.user.service.company.ICompanyService;
import com.hoshiicloud.user.service.platform.PlatformService;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * <p>
 * 平台 服务实现类
 * </p>
 *
 * @author yl
 * @since 2019-04-18
 */
@Slf4j
@Service
public class PlatformServiceImpl extends ServiceImpl<PlatformMapper, Platform> implements PlatformService {


    @Autowired
    private IdGeneratorService idGeneratorService;

    @Autowired
    private ICompanyService companyService;


    /**
     * @return IPage
     * @description: 分页查询平台信息
     */
    @Logable(authName = "平台管理-平台信息管理-分页查询平台信息")
    @Cacheable(cacheNames = "Platform",key ="#root.targetClass.simpleName+':'+#root.methodName+':'+#key")
    public IPage<Platform> getPage(IPage page, QueryWrapper queryWrapper,String key) {
        return baseMapper.selectPage(page, queryWrapper);
    }

    @Override
    public Platform platformName(String platformName) {
        try {
            Wrapper wrapper = Wrappers.<Platform>query().lambda().eq(Platform::getPlatformName, platformName);
            return this.getOne(wrapper);
        }catch (Exception e) {
            log.error("查询平台信息异常",e);
            throw new ServiceException("查询平台信息异常");
        }
    }

    /**
     * @param id
     * @return com.hoshiicloud.platform.entity.Platform
     * @description: 根据id查询平台信息
     */
    @Cacheable(cacheNames = "Platform", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#id")
    @Logable(authName = "平台管理-平台信息管理-查询平台信息")
    public Platform getById(Long id) {
        return baseMapper.selectById(id);
    }


    /**
     * @param entity
     * @return boolean
     * @description: 新增平台
     */
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "Platform", allEntries = true)
    @Logable(authName = "平台管理-平台信息管理-新增平台信息")
    public boolean save(Platform entity){
        String id = idGeneratorService.queryGeneratorId();
        if (StringUtils.isBlank(id)) {
            throw new ServiceException(CommonError.error("id生成失败,新增失败"));
        }
        Company company = companyService.getById(entity.getCompanyId());
        if( null == company){
            throw new ServiceException(CommonError.error("新增失败,公司信息有误"));
        }

        entity.setId(Long.parseLong(id));
        entity.setCreatedTime(new Date());

        int row = baseMapper.insert(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("新增失败"));
        }
        return true;
    }



    /**
     * @return boolean
     * @description: 修改平台信息
     */
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "Platform", allEntries = true)
    @Logable(authName = "平台管理-平台信息管理-修改平台信息")
    public boolean updateById(Platform entity){
        entity.setModifiedTime(new Date());
        int row = baseMapper.updateById(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("修改失败"));
        }
        return true;
    }


}
